package com.hmall.order.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hmall.order.dto.OrderDto;
import com.hmall.order.pojo.Order;
import com.hmall.order.service.OrderService;
import com.hmall.order.service.WxAndZfbPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private WxAndZfbPayService wxAndZfbPayService;

    /**
     * 分页查询订单
     *
     * @param page 页码
     * @param size 每页大小
     * @return 订单列表
     */
    @GetMapping
    public IPage<Order> pageQuery(@RequestParam int page, @RequestParam int size) {
        return orderService.pageQuery(page, size);
    }

    /**
     * 创建订单
     *
     * @param orderDto
     * @return
     */
    @PostMapping
    public String insert(@Validated @RequestBody OrderDto orderDto) {
        String orderId = orderService.insert(orderDto);
        return orderId;
    }

    /**
     * 更新订单
     *
     * @param order 订单
     * @return 订单
     */
    @PutMapping
    public String update(@RequestBody Order order) {
        return orderService.update(order);
    }

    /**
     * 查询订单
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Order getById(@PathVariable Long id) {
        log.info("查询订单：{}", id);
        return orderService.getById(id);
    }

    /**
     * 添加一个方法，处理订单支付请求 /order/pay/2/23432453543
     * type：支付类型  1支付宝支付 2微信支付 3余额支付
     */
    @GetMapping("/pay/{type}/{orderId}")
    public String orderPay(@PathVariable Integer type, @PathVariable Long orderId, HttpServletResponse request) {
        if (type == 1) {
            // 支付宝支付
            String zfbPay = wxAndZfbPayService.zfbPay(type, orderId, request);
            return zfbPay;
        } else if (type == 2) {
            // 微信支付
            String codeUrl = wxAndZfbPayService.wxPay(type, orderId);
            return codeUrl;
        }
        return "支付失败";
    }

    /**
     * 支付宝异步回调接口
     *
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/notify")  // 注意这里必须是POST接口
    public String payNotify(HttpServletRequest request) {
        log.info("=================== 支付宝支付成功之后回调方法 ===================");
        String s = wxAndZfbPayService.payNotify(request);
        return s;
    }

    /**
     * 添加一个方法，处理微信回调 /order/pay/notify
     *
     * @param request
     */
    @PostMapping("/pay/notify")
    public Map<String, String> wxPayNotify(HttpServletRequest request) throws IOException {
        log.info("=================== 微信支付成功之后回调方法 ===================");
        Map<String, String> map = wxAndZfbPayService.wxPayNotify(request.getInputStream());
        return map;
    }

}